/*
实现一个3*3的矩阵类，支持如下操作符： 
1. 运算类双目操作符：+ - * 
2. 赋值类双目操作符：+= -= *= 
3. 单目操作符：-(相反数) 
4. 输出操作符：<<  
 *
 */
#include<iostream>
using namespace std;

class Matrix{
	private:
		int m_array[3][3];
	public:
		Matrix(){
			for(int i = 0; i < 3; i++){
				for(int j = 0; j < 3; j++){
					m_array[i][j] = 0;
				}
			}
		}
		Matrix(int array[][3]){
			for(int i = 0; i < 3; i++){
				for(int j = 0; j < 3; j++){
					m_array[i][j] = array[i][j];
				}
			}
		}
		void print(){
			cout << *this;
		}
		//+重载
		const Matrix operator+(const Matrix& c) const{
			Matrix tmp;
			for(int i = 0; i < 3; i++){
				for(int j = 0; j < 3; j++){
					tmp.m_array[i][j] = m_array[i][j] + c.m_array[i][j];
				}
			}
			return tmp;
		}
		//-重载
		const Matrix operator-(const Matrix& c) const{
			Matrix tmp;
			for(int i = 0; i < 3; i++){
				for(int j = 0; j < 3; j++){
					tmp.m_array[i][j] = m_array[i][j] - c.m_array[i][j];
				}
			}
			return tmp;
		}
		//*重载
		const Matrix operator*(const Matrix& c) const{
			Matrix tmp;
			for(int i = 0; i < 3; i++){
				for(int j = 0; j < 3; j++){
					for(int k = 0; k < 3; k++){
						tmp.m_array[i][j] += m_array[i][k] * c.m_array[k][j];
					}
				}
			}
			return tmp;
		}
		//+=重载
		Matrix& operator+=(const Matrix& c){
			*this = *this + c;
			return *this;
		}
		//-=重载
		Matrix& operator-=(const Matrix& c){
			*this = *this - c;
			return *this;
		}
		//*=重载
		Matrix& operator*=(const Matrix& c){
			*this = *this * c;
			return *this;
		}
		//-取负
		const Matrix operator-(){
			Matrix a;
			return a - *this;
		}
		//cout重载输出Matrix
		friend ostream& operator<<(ostream& cout, const Matrix& c){
			for(int i = 0; i < 3; i++){
				for(int j = 0; j < 3; j++){
					cout << c.m_array[i][j] << " ";
				}
				cout << endl;
			}
			cout << endl;
			return cout;
		}

};

int main(){
	int array1[][3] = {1,2,3,4,5,6,7,8,9};
	int array2[][3] = {9,8,7,6,5,4,3,2,1};
	Matrix m1(array1);
	Matrix m2(array2);
	m1.print();
	m2.print();

	//运算类双目操作符
	Matrix m3 = m1 + m2;
	m3.print();
	Matrix m4 = m1 - m2;
	m4.print();
	Matrix m5 = m1 * m2;
	m5.print();

	//赋值类双目操作符
	m1 += m2;
	m1.print();
	m1 -= m2;
	m1.print();
	m1 *= m2;
	m1.print();

	//取负
	Matrix m6 = -m1;
	m6.print();

	return 0;
}

